package commons.base.luncene;

import java.io.IOException;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.queryparser.classic.ParseException;

import commons.base.db.Database;
import commons.base.db.Databases;
import commons.base.luncene.IndexUser.UserDoc;

public class IndexExample {
	
	public static void main(String[] args) throws Exception {
//		clearIndex("/data/log/indexes/user");
//		createIndex();
//		
//		readIndex();
		
		testUserName();
//		genIndex();
	}
	
	public static void testUserName()throws Exception{
		IndexUser indexUser = IndexManager.getIndexService(IndexUser.class);
//		indexUser.update(new UserDoc("1", "u1", "哈哈")).update(new UserDoc("2", "u2", "中国"));
		
//		List<UserDoc> list = indexUser.searchByUserName(".*yuan.*");
		List<UserDoc> list = indexUser.searchByCaptainName(".*火车.*");
		System.err.println(list);
		for(UserDoc doc : list){
			System.err.println(doc.getUserId() + ", " + doc.getUserName() + ", " + doc.getCaptainName());
		}
		
		indexUser.close();
	}
	
	public static void createIndex() throws IOException{
		String indexPath = "/data/log/indexes";
		
		IndexDir indexServer = new IndexDir(indexPath);
		
		indexServer.getUpdater()
				.addDocument(buildDoc("中国", 1))
				.addDocument(buildDoc("中国人", 2))
				.addDocument(buildDoc("哈哈", 3))
				.commit();

		indexServer.close();
	}
	
	public static void readIndex() throws IOException, ParseException{
		String indexPath = "/data/log/indexes";
		
		IndexDir indexServer = new IndexDir(indexPath);
		
		List<Document> list = indexServer.getSearcher().search("username", ".*哈哈.*", 1000);
		System.err.println(list);
		for(Document doc : list){
			System.err.println(doc.get("username") + ", " + doc.get("userId"));
		}
		
		indexServer.close();
	}
	
	public static Document buildDoc(String username, long userId){
		Document doc = new Document();
		
		doc.add(new StringField("username", username, Field.Store.YES));
		doc.add(new StringField("userId", userId + "", Field.Store.YES));
		
		return doc;
	}
	
	public static void clearIndex(String indexPath) throws IOException{
		new IndexDir(indexPath).getUpdater().deleteAll().close();
	}
	
	public static void genIndex() throws Exception{
		Database database = Databases.getDatabase("132");
		IndexUser indexUser = IndexManager.getIndexService(IndexUser.class);
		
		for(int i=0; i<=99; i++){
			String sql = "select id as userId,username as userName,captainName from user_" + i;
//			String sql = "select id as userId,username as userName,captainName from user_0 limit 10";
			List<UserDoc> userList = database.queryModelList(sql, UserDoc.class);
			for(UserDoc user : userList){
				System.err.println(user.getUserId() + "," + user.getUserName() + "," + user.getCaptainName());
				indexUser.update(user);
			}
		}
		
		Databases.shutdown();
		IndexManager.shutdown();
	}
}
